package dice.sinanya.tools.makedata;

import dice.sinanya.entity.EntityReply;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.ReplyRecursiveException;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static dice.sinanya.system.MessagesSystem.*;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityGame;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;

public class ModeForReply {
    EntityTypeMessages entityTypeMessages;

    public ModeForReply(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    public ModeForReply() {
    }

    public void insert(String key, String values) throws ReplyRecursiveException, DocumentException {
        ArrayList<EntityReply> replies = new ArrayList<>();

        for (Map.Entry<Long, HashMap<String, ArrayList<EntityReply>>> allReplies : AllReply.entrySet()) {
            checkRecursive(allReplies.getValue(), key, values, allReplies.getKey());
        }
        checkRecursive(replyMap, key, values, loginInfo.getLoginId());
        for (String value : values.split("\\|")) {
            EntityReply reply = new EntityReply();
            reply.setValue(value);
            reply.setWeight(1);
            replies.add(reply);
        }
        if (key.equals("自身")){
            key=loginInfo.getLoginName();
            replyMap.put(key,replies);
            key=String.format("[CQ:at,qq=%s,display=@%s]",loginInfo.getLoginId(),loginInfo.getLoginName());
            replyMap.put(key,replies);
            key= String.valueOf(loginInfo.getLoginId());
            replyMap.put(key,replies);
            flush();
            return;
        }
        replyMap.put(key, replies);
        flush();
    }

    public void append(String key, String values) {
        ArrayList<EntityReply> replies = replyMap.getOrDefault(key, new ArrayList<>());
        EntityReply reply = new EntityReply();
        reply.setValue(values);
        reply.setWeight(1);
        reply.setPublicMode(true);
        replies.remove(reply);
        replies.add(reply);
        replyMap.put(key, replies);
        flush();
    }

    private void checkRecursive(HashMap<String, ArrayList<EntityReply>> checked, String key, String values, long qq) throws ReplyRecursiveException {
        ArrayList<String> valueList = new ArrayList<>();
        ArrayList<String> keyList = new ArrayList<>();
        for (Map.Entry<String, ArrayList<EntityReply>> map : checked.entrySet()) {
            keyList.add(map.getKey());
            for (EntityReply entityReply : map.getValue()) {
                valueList.add(entityReply.getValue());
            }
        }
        if (valueList.contains(key)) {
            throw new ReplyRecursiveException(entityTypeMessages, key, qq);
        }
        for (String value : values.split("\\|")) {
            if (keyList.contains(value)) {
                throw new ReplyRecursiveException(entityTypeMessages, value, qq);
            }
        }
    }

    public void insert(String key, ArrayList<EntityReply> replies) throws DocumentException {
        replyMap.put(key, replies);
        flush();
    }

    public void remove(String key) throws DocumentException {
        replyMap.remove(key);
        flush();
    }

    public String get(String key) {
        ArrayList<String> values = new ArrayList<>();
        for (EntityReply value : replyMap.getOrDefault(key, new ArrayList<>())) {
            values.add(value.getValue());
        }
        return stringFormatById("%s %s", key, StringUtils.join(values, "|"));
    }

    public String list() {
        return new XmlFromReply(replyMap).getXml();
    }

    private void flush() {
        entityGame.setReply(list());
        new dice.sinanya.db.properties.game.InsertProperties().insertProperties(entityGame);
    }
}
